/* eslint-disable eqeqeq */
import actionInfo from '../action'
import dayjs from 'dayjs'
import { orderAdd, orderQuery } from '../service/orderService'

function orderInfoReducer(state = { orderList: [] }, action) {
    switch (action.type) {
        case actionInfo.order.ADD:
            return { orderList: [...state.orderList, action.payload] }
        case actionInfo.order.QUERY:
            return { orderList: action.payload }
        default:
            return state
    }
}
export const asyncOrderQuery = (params) => async (dispatch) => {
    let { data } = await orderQuery(params);//做异步请求
    let orderlist = data.msg.map(item => ({ ...item, date: dayjs(item.date).format('YYYY-MM-DD') }))
    dispatch(orderInfoQuery(orderlist));//
}
const orderInfoQuery = (payload) => {
    return {
        type: actionInfo.order.QUERY,
        payload
    }
}
export const asyncOrderAdd = (params) => async (dispatch) => {
    let { data } = await orderAdd(params);//做异步请求
    if (data.code == 1) {
        let orderlist = data.msg.map(item => ({ ...item, date: dayjs(item.date).format('YYYY-MM-DD') }))
        dispatch(orderInfoAdd(...orderlist));
    }
}
const orderInfoAdd = (payload) => {
    return {
        type: actionInfo.order.ADD,
        payload
    }
}
export default orderInfoReducer